<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
if($_data["Bet_Type"] != "")$data["Bet_Type_n"] = (int)$_data["Bet_Type"];
$data["LottoID_n"] = $_data["LottoID"];
$data["WagerType_i"] = $_data["WagerType"];
$data["PayupDate_x"][0] = $_data["PayupDate_0"];
$data["PayupDate_x"][1] = $_data["PayupDate_1"];
if(!empty($_data["GambleID"]))$data["GambleID_n"] = $_data["GambleID"];

if($isLimit){
	$data["PayupDate_x"][0] = getPayupDateOfAccountSearchLimit($data["PayupDate_x"][0], $isSevenDay);
	if($data["PayupDate_x"][0] > $data["PayupDate_x"][1]){
		$GLOBALS["errormessage"] = "Over search date range!!";
		ErrMsgOutputHTML("Error", "", 1);
	}
}else{
	if($data["PayupDate_x"][0] > $data["PayupDate_x"][1]){
		$tmp = $data["PayupDate_x"][1];
		$data["PayupDate_x"][1] = $data["PayupDate_x"][0];
		$data["PayupDate_x"][0] = $tmp;
	}
}

$data["MemID_n"] = $_data["MemID"];
if(strlen($_data["WagerType"]) > 0){// $_data["Types"] != ""
	switch($_data["Types"]){
		case "Director":
			$data["StockID_n"] = $_data["StockID"];
		case "Stock":
			$data["MainID_n"] = $_data["MainID"];
		case "MainAgent":
			$data["SubID_n"] = $_data["SubID"];
			break;
	}
	//$data["StockID_n"] = $_data["StockID"];
	//$data["MainID_n"] = $_data["MainID"];
	//if($_data["SubID"] != "")$data["SubID_n"] = $_data["SubID"];
}
//$data["isPayOff_n"] = 0;

$strSubmitForm = "ListForm";
$n = "select ID, LottoID, MemCommission as MC, Bet_Type, GambleID,  MemStake, SubIncome, isPayOff, WagerType";

$strSQLs = SelectSQL ( $data, "`bet_BetList`");
$strSQLs .= " and isPayOff > -2 ";
$strSQL = str_replace("select *", $n, $strSQLs);
$strSQLtemp = str_replace("select * from", "select COUNT(*) as total from", $strSQLs);

$n = "select Count(*) as Nos, SUM(MemStake) as MS, SUM(SubResult) as SR";
$strSQLs = str_replace("select *",$n,$strSQLs)." and isPayOff = 0 group by SubID";
$Sub_Nos = 0;
$Sub_MS = 0;
$Sub_SR = 0;

if($RS = @PMA_mysql_query($strSQLs, $$_Conn)){
	if(PMA_mysql_num_rows($RS) > 0){
		$Sub_Nos = PMA_mysql_result($RS, 0, "Nos");
		$Sub_MS = PMA_mysql_result($RS, 0, "MS");
		$Sub_SR = PMA_mysql_result($RS, 0, "SR");
	}
	@mysql_free_result($RS);
}
$strSQL .=" order by ID desc";

if($RS = @PMA_mysql_query($strSQLtemp, $$_Conn)){
	$Rows = PMA_mysql_fetch_object($RS);
	$intTotal = ($Rows -> total > 0)?$Rows -> total:false;
}
unset($strSQLtemp);

$intTotalPage = $_data["totalpage"];
$Page = (int)$_data["Page"];
$intPageNo = 20;												//一頁所要顯示的筆數
$intTotalPage = ceil($intTotal / $intPageNo);		//總共幾頁
$intPageNow = (empty($Page))? 1:$Page;		//假如目前第幾頁為空值，其值為第一頁
$intPageNow = ($intPageNow > $intTotalPage)? $intTotalPage:$intPageNow;	//目前第幾頁，假如大於總頁數，為總頁數
$intPageNow = ($intPageNow < 1)? 1:$intPageNow;	//目前第幾頁，假如小於1，為第 1 頁
if($intPageNow % 10 > 0){
	$intFirstPage = floor($intPageNow / 10)*10 + 1;
	$intLastPage = (floor($intPageNow / 10) + 1)*10;
}else{
	$intFirstPage = floor(($intPageNow - 1) / 10)*10 + 1;
	$intLastPage = (floor(($intPageNow - 1) / 10) + 1)*10;
}

$intLastPage = ($intLastPage > $intTotalPage)? $intTotalPage:$intLastPage;

$strSQL = $strSQL." limit ".(($intPageNow - 1)*$intPageNo).",".$intPageNo."";
$arrRows = array();
$arrName = array();
$arrID = array();
$arrGambleID = array();
$arrConRows = array();
//print $strSQL;
if($RS = @PMA_mysql_query($strSQL, $$_Conn)){
	while($Rows = PMA_mysql_fetch_object($RS)){
		$arrRows[$Rows -> ID] = $Rows;
		$arrID[] = $Rows -> ID;
		$arrGambleID[$Rows -> GambleID] = $Rows -> GambleID;
	}
}else{
	print "<!--".PMA_mysql_error($$_Conn)."//-->";
}
@mysql_free_result($RS);

if(sizeof($arrID) > 0){
	$strSQL = "select ID, BetDate as BD, BetTime as BT, Content, MemAccount, OneStake from`bet_BetListContent` where ID in (".join(",", $arrID).")";
	if($RS = @PMA_mysql_query($strSQL, $$_Conn)){
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrConRows[$Rows -> ID] = $Rows;
		}
	}
}

//取得開獎結果
if(sizeof($arrGambleID)){
	$strSQL = "select a.ID, a.LottoID, a.GameDate, b.NumberNormal, b.NumberSpecial from `bet_Gamble` a, `bet_GambleScore` b where a.ID = b.GambleID and a.ID in (".implode(",", $arrGambleID).") and a.isEnd = 1";
	if($RS = @PMA_mysql_query($strSQL, $$_Conn)){
		$arrColor = array("#FF0000","#0000FF", "#00FF00");

		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrColorSet = explode(",", $_arrLotto[$Rows -> LottoID]["ColorSet"]);
			$arrAnimals = getAnimalsParamsByDate($Rows -> LottoID, $Rows -> GameDate);
			$arrAnimals = getExChangeAnimalsParams($arrAnimals);

			$strScore = "";
			$arrNN = explode(",", $Rows -> NumberNormal);
			$intSN = $Rows -> NumberSpecial;
			switch($_arrLotto[$Rows -> LottoID]["LType"]){
				case 1:
					foreach($arrNN as $k => $v){
						$strColor = $arrColor[$arrColorSet[((int)$v - 1)]];
						$arrNN[$k] = "<span style='color:".$strColor."'>".sprintf("%02s", $v)."</span> ".$_arrAnimals[$arrAnimals[$v]]."";
					}
					$strColor = $arrColor[$arrColorSet[((int)$intSN - 1)]];
					$strScore .= implode(", ", $arrNN)." + <span style='color:".$strColor."'>".sprintf("%02s", $intSN)."</span> ".$_arrAnimals[$arrAnimals[$intSN]]."";
					break;

				case 4:
					foreach($arrNN as $k => $v){
						$arrNN[$k] = "<span>".sprintf("%02s", $v)."</span>";
					}
					$strScore .= implode(", ", $arrNN)." + <span>".sprintf("%02s", $intSN)."</span>";
					break;

				default:
					foreach($arrNN as $k => $v){
						$arrNN[$k] = "<span>".$v."</span>";
					}
					$strScore .= implode(", ", $arrNN);
					break;
			}

			$arrGameRows[$Rows -> ID] = "(".$strScore.")";
		}
	}
}
?>